package complet.app.tool.parameterevaluator;

import complet.db.Retrieval;
import complet.helper.ConfigHelper;
import complet.helper.IOHelper;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.util.Set;

public class ParameterEval {

    public static String IN_PATH = "D:/Users/Admin/workspace/Jufo/Rawdata/Wiki/wiki.txt";
    public static String OUT_PATH = "D:/Users/Admin/workspace/Jufo/Data/results_interval_1.txt";
    public static String DB_PATH = "D:/Users/Admin/workspace/Jufo/DB_1.0.db";
    public static String LOG_PATH = "D:/Users/Admin/workspace/Jufo/Data/log.txt";
    public static int max_sentences = 500000;
    private static Retrieval ret;

    public static void setSentences(int sentence_count) {
        max_sentences = sentence_count;
    }

    public static void main(String[] args) {
        try {
            ConfigHelper.DB_PATH = DB_PATH;
            ConfigHelper.IN_PATH = IN_PATH;
            ConfigHelper.BUFFER = true;

            if (ConfigHelper.DB_PATH.isEmpty()) {
                IOHelper.pln("DB_PATH is empty!");
                return;
            }
            IOHelper.pln("Connect to database (" + ConfigHelper.DB_PATH + ")...");
            IOHelper.pln("Buffer Database...");

            ret = new Retrieval();
            //
            // Evaluator eval = new Evaluator();
            // eval.setAnz_stnce(max_sentences);
            // int counter = 0;
            // while (counter <= max_sentences) {
            // for (int i = 0; i < 10; i++) {
            // ret.setP1(Math.random());
            // ret.setP2(Math.random());
            // ret.setP3(Math.random());
            // ret.setP4(Math.random());
            //
            // eval.eval();
            // }
            //
            // }
            FileReader file = new FileReader(IN_PATH);
            BufferedReader in = new BufferedReader(file);
            String strLine = "";

            int sentence_counter = 0;
            String result = "";
            String log = "";
            while (in.readLine() != null && sentence_counter < max_sentences) {
//				long startTime = System.nanoTime();
                sentence_counter++;
                strLine = in.readLine();
                strLine = strLine.replaceAll("[^a-zA-Z ]", "");
                String[] words = strLine.toLowerCase().split(" ");
                if (words.length >= 5) {
                    //random selection of 4 words
                    String[] input = new String[4];
                    int pos = (int) (Math.random() * words.length) - 4;
                    if (pos < 0) {
                        pos = 0;
                    }
                    System.arraycopy(words, pos, input, 0, 4);
                    String wanted_word = words[pos + 4];
                    ret.setSentence(input, wanted_word.substring(0, 1));

                    log += "Satz " + sentence_counter + ":\n";
                    for (int i = 0; i < input.length; i++) {
                        log += input[i] + " ";
                    }
                    log += "- " + wanted_word + "\n";
                    Set<String> set;
//					result += sentence_counter + "\n";

                    double[] p1 = new double[100];
                    double[] p2 = new double[100];
                    double[] p3 = new double[100];
                    double[] p4 = new double[100];

                    int interval_up = 1000000;
                    int interval_low = 0;
                    int change_counter = 3;
                    int best_result = 1000000;
                    int best_try = 0;
                    int i = 0;
                    while (i < 100 && change_counter != 0) {

                        p1[i] = Math.random();
                        p2[i] = Math.random();
                        p3[i] = Math.random();
                        p4[i] = Math.random();

                        ret.setP1((int) p1[i] * 100);
                        ret.setP2((int) p2[i] * 100);
                        ret.setP3((int) p3[i] * 100);
                        ret.setP4((int) p4[i] * 100);

                        ret.eval();
                        set = ret.getSortedKeySet(1);

                        //get wordposition in resultset
                        int result_pos = 0;
                        for (String key : set) {
                            result_pos++;
                            if (key.equals(wanted_word)) {
                                break;
                            }
                        }

                        if (result_pos > interval_low) {
                            interval_low = result_pos;
                            change_counter = 3;
                        }
                        if (result_pos < interval_up) {
                            interval_up = result_pos;
                            change_counter = 3;
                        } else {
                            change_counter--;
                        }

                        if (result_pos < best_result) {
                            best_result = result_pos;
                            best_try = i;
                        }

                        i++;
//						System.out.println(p1[best_try] + "\tresult: " + result_pos + "\tbest result: " + best_result + ", best try: " + best_try + ", interval: " + (interval_low-interval_up));
                    }
//					System.out.println();

                    int interval_length = interval_low - interval_up;

                    result += p1[best_try] + "\t" + p2[best_try] + "\t" + p3[best_try] + "\t" + p4[best_try] + "\t" + best_result + "\t" + interval_length + "\n";
//					long endTime = System.nanoTime();
//					double time = (double) ((endTime - startTime)/1000000);
                    if (sentence_counter % 10 == 0) {
                        IOHelper.pln("Satz: " + sentence_counter);
                    }

                }
                if (sentence_counter % 10000 == 9999) {
                    IOHelper.pln("####### (Evaluator) RESULTS - STILL IN PROGRESS... ########");
                    writeResult(result);
                    logResult(log);
                    result = "";
                    log = "";
                }
            }
            IOHelper.pln("####### (Evaluator) RESULTS - FINISHED... ########");
            writeResult(result);
            logResult(log);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void logResult(String output) {
        try {
            File file = new File(LOG_PATH);
            FileWriter out = new FileWriter(file, true);

            out.write(output + "\n");

            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void writeResult(String output) {
        try {
            File file = new File(OUT_PATH);
            FileWriter out = new FileWriter(file, true);

            out.write(output);

            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
